package vip.xiaonuo.smzq.modular.dictionary.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import vip.xiaonuo.common.pojo.CommonResult;
import vip.xiaonuo.smzq.core.gson.GsonUtil;
import vip.xiaonuo.smzq.core.tools.excel.ExcelImportUtil;
import vip.xiaonuo.smzq.modular.dictionary.entity.Cb09Hkcw;
import vip.xiaonuo.smzq.modular.dictionary.entity.excelEnt.Cb09HkcwExc;
import vip.xiaonuo.smzq.modular.dictionary.service.ICb09HkcwService;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;


/**
 * 含矿层位
 *
 * @author eomer
 * @since 2024-02-21
 */
@Api(tags = "含矿层位")
@RestController
@RequestMapping("/dictionary/cb09Hkcw")
@Validated
public class Cb09HkcwController {

    @Resource
    private ICb09HkcwService iCb09HkcwService;

    /**
     * 添加含矿层位
     */
    @ApiOperation("添加含矿层位")
    @PostMapping("/add")
    public CommonResult<String> add(@RequestBody @Valid Cb09Hkcw cb09Hkcw) {
        iCb09HkcwService.save(cb09Hkcw);
        return CommonResult.ok();
    }

    /**
     * 删除含矿层位
     */
    @ApiOperation("删除含矿层位")
    @PostMapping("/delete")
    public CommonResult<String> delete(@ApiParam(name = "pkid", value = "主键") @RequestParam String pkid) {
        iCb09HkcwService.removeById(pkid);
        return CommonResult.ok();
    }

    /**
     * 编辑含矿层位
     */
    @ApiOperation("编辑含矿层位")
    @PostMapping("/edit")
    public CommonResult<String> edit(@RequestBody @Valid Cb09Hkcw cb09Hkcw) {
        iCb09HkcwService.updateById(cb09Hkcw);
        return CommonResult.ok();
    }

    /**
     * 含矿层位分页查询
     */
    @ApiOperation("含矿层位分页查询")
    @GetMapping("/list")
    public CommonResult<Page<Cb09Hkcw>> page() {
        return CommonResult.data(iCb09HkcwService.page(new Cb09Hkcw()));
    }

    /**
     * 导入
     */
    @ApiOperation("导入")
    @PostMapping("/import")
    public CommonResult<String> importExcel(@RequestParam("file") MultipartFile file) {
        try {
            List<Cb09HkcwExc> list = ExcelImportUtil.readMultipartFile(file, Cb09HkcwExc.class);
            List<Cb09Hkcw> cb01Zhs = GsonUtil.ObjectToList(list, Cb09Hkcw.class);
            iCb09HkcwService.saveBatch(cb01Zhs);
            return CommonResult.ok();
        } catch (Exception e) {
            return CommonResult.error(e.getMessage());
        }
    }
}
